
function post(invoiceNo) {
    var method = 'POST';
    var req = new XMLHttpRequest();
    
    req.open(method, common.url, true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.POST_OK) {
                var res = req.responseText;
                resetParams();
//                alert(method + ' 成功: ' + res);
                alert('新規の請求書を作成しました');
              
				entity = getDefaultJson(invoiceNo);
                
                //alert(JSON.stringify(entity));

                renderSheet();
            } else if (res.status.code == HTTP_STATUS_CODE.POST_NG) {
                alert(method + ' 失敗: invoiceNoが重複しています');
            } else {
                var res = req.responseText;
                alert(method + ' エラーxx ' + res.status.code);
//                alert(method + ' エラー ' + res);
            }
        } else if (req.readyState >= 1) {
        }
    }
    
    req.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');

    
    var entityPost = {};
    entityPost['invoice'] = {};
    entityPost['invoice']['invoiceNo'] = invoiceNo;
    req.send(JSON.stringify(entityPost));
}


function read(key, val) {

    var method = 'GET';
    var req = new XMLHttpRequest();

    req.open(method, common.url + '?' + key + '=' + encodeURI(val), true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.GET_OK) {
                var res = req.responseText;
                //resetParams();

                //alert(res);
                //eval('entity=' + res);
                
                //var tmp = {};
                eval('entity=' + res);

                if (entity['invoice']['deleted'] == 1) {
                    createDeleted();
                } else {
                    renderSheet(); 
                }
            } else if (req.status == HTTP_STATUS_CODE.GET_NG) {
                alert(method + ' 失敗');
            } else {
                var res = req.responseText;
                alert(method + ' エラー: ' + res);
            }
        } else if (req.readyState >= 1) {
        }
    }

    req.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
    req.send(null);
}


function pdf(invoiceNo) {

    var method = 'GET';
    var req = new XMLHttpRequest();
    
    var url = 'http://pdf.latest.reflex-itext.appspot.com/pdfservicegae?template=' + document.URL + '&entity=http://invoice.latest.reflexcontainer.appspot.com/invoice?invoiceNo=' + entity['invoice']['invoiceNo'] + '&xml'

    req.open(method, url, true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.GET_OK) {
                var res = req.responseText;
                //resetParams();
//                alert(method + ' 成功');

            } else if (req.status == HTTP_STATUS_CODE.GET_NG) {
                alert(method + ' 失敗');
            } else {
                var res = req.responseText;
                alert(method + ' エラー: ' + res);
            }
        } else if (req.readyState >= 1) {
        }
    }

    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    req.send(null);
}





function getInvoiceList(nextId, pageSize) {
    if (nextId == null) {
        nextId = 0;
    }

    if (pageSize == null) {
        pageSize = 10;
    }

    var method = 'GET';
    var req = new XMLHttpRequest();

    req.open(method, common.url + '?nextid=' + nextId + '&pagesize=' + pageSize , true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.GET_OK) {
                var res = req.responseText;
//                alert('請求書一覧を取得しました');
                //alert(res);
                eval('var tmp=' + res);
                entities = {};       
                entities = tmp['invoiceBase']['invoice'];
            } else if (req.status == HTTP_STATUS_CODE.GET_NG) {
                alert('getInvoiceList(' + nextId + ', ' + pageSize + ') 失敗');
            } else {
                var res = req.responseText;
                alert('getInvoiceList(' + nextId + ', ' + pageSize + ') エラー: ' + res);
            }
        } else if (req.readyState >= 1) {
        }
    }

    req.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
    req.send(null);
}





function update(_entityUpdate) {
    var method = 'PUT';
    var req = new XMLHttpRequest();

    req.open(method, common.url, true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.PUT_OK) {
                var res = req.responseText;

                //resetParams();
                //eval('entity=' + res);
                
                //更新に成功した場合はrevisionをインクリメント
                var revision = parseInt(entity['invoice']['revision'], 10);
                entity['invoice']['revision'] = ++revision;

//                alert(method + ' 成功: (Revision:' + entity['invoice']['revision'] + ')');
                alert(' 更新しました: (Revision:' + entity['invoice']['revision'] + ')');

                renderSheet();
            } else if (req.status == HTTP_STATUS_CODE.PUT_NG) {
                alert(method + ' 失敗');
            } else {
                alert(method + ' エラー');
            }
        } else if (req.readyState >= 1) {

        }
    }
    
    req.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
    req.send(JSON.stringify(_entityUpdate));
}

function del(invoiceNo) {
    
    var method = 'DELETE';
    var req = new XMLHttpRequest();

    //req.open(method, common.url, true);
    req.open(method, common.url + '?invoiceNo=' + invoiceNo, true);
    req.onreadystatechange = function(){
        if (req.readyState == 4) {
            if (req.status == HTTP_STATUS_CODE.DELETE_OK) {
                var res = req.responseText;
                alert('削除しました');
            } else if (req.status == HTTP_STATUS_CODE.DELETE_NG) {
                alert(method + ' 失敗');
            } else {
                alert(method + ' エラー');
            }
        } else if (req.readyState >= 1) {
        }
    }
    
    req.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
    //req.send(invoiceNo);
    req.send(null);
}




function debug(val) {
    var debug = document.getElementById('debug');
    debug.innerHTML = val;
}

